import java.util.Objects;

/**
 * @author VernHe
 * @date 2021年05月14日 16:07
 */
public class Solution_072 {
    public static void main(String[] args) {
        new Solution_072().minDistance2("horse","ros");
    }
    public int minDistance1(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();
        System.out.println("len1: " +len1 + "  len2: " + len2);
        int[][] dp = new int[len1 + 1][len2 + 1];
        // 初始化
        for (int i = 1; i <= len2; i++) {
            dp[0][i] = i;
        }
        for (int i = 1; i <= len1; i++) {
            dp[i][0] = i;
        }

        // 循环
        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
                if (word1.charAt(i-1) == word2.charAt(j-1)) {
                    // 什么都不需要做
                    dp[i][j] = dp[i-1][j-1];
                } else {
                    dp[i][j] = Math.min(Math.min(dp[i-1][j-1],dp[i][j-1]),dp[i-1][j]) + 1;
                }
            }
        }

        return dp[len1][len2];
    }

    public int minDistance2(String word1, String word2) {
        // 参数判断

        char[] chars1 = word1.toCharArray();
        char[] chars2 = word2.toCharArray();
        int row = chars1.length;
        int col = chars2.length;
        // 初始化dp数组
        int[][] dp = new int[row + 1][col + 1];
        dp[0][0] = 0;
        // 初始化第一行第一列
        for (int i = 1; i <= row; i++) {
            dp[i][0] = i;
        }
        for (int i = 1; i <= col; i++) {
            dp[0][col] = i;
        }
        // 从第一行第一列开始
        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <= col; j++) {
                if (chars1[i - 1] == chars2[j - 1]) {
                    dp[i][j] = dp[i-1][j-1];
                } else {
                    dp[i][j] = Math.min(dp[i-1][j-1],Math.min(dp[i][j-1],dp[i-1][j])) + 1;
                }
            }
        }
        return dp[row][col];
    }
}
